<<<<<<< HEAD
import { login, getProfile, getUserDetailById } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router from '@/router'

const state = {
  // 刷新页面时,先看看本地储存有没有备份
  // token: localStorage.getItem('token') || ''
  token: getToken() || '',
  profile: {}
=======
// import { login, logout, getInfo } from '@/api/user'
// import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

// const getDefaultState = () => {
//   return {
//     token: getToken(),
//     name: '',
//     avatar: ''
//   }
// }

// const state = getDefaultState()

// const mutations = {
//   RESET_STATE: (state) => {
//     Object.assign(state, getDefaultState())
//   },
//   SET_TOKEN: (state, token) => {
//     state.token = token
//   },
//   SET_NAME: (state, name) => {
//     state.name = name
//   },
//   SET_AVATAR: (state, avatar) => {
//     state.avatar = avatar
//   }
// }

// const actions = {
//   // user login
//   login({ commit }, userInfo) {
//     const { username, password } = userInfo
//     return new Promise((resolve, reject) => {
//       login({ username: username.trim(), password: password }).then(response => {
//         const { data } = response
//         commit('SET_TOKEN', data.token)
//         setToken(data.token)
//         resolve()
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // get user info
//   getInfo({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       getInfo(state.token).then(response => {
//         const { data } = response

//         if (!data) {
//           return reject('Verification failed, please Login again.')
//         }

//         const { name, avatar } = data

//         commit('SET_NAME', name)
//         commit('SET_AVATAR', avatar)
//         resolve(data)
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // user logout
//   logout({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       logout(state.token).then(() => {
//         removeToken() // must remove  token  first
//         resetRouter()
//         commit('RESET_STATE')
//         resolve()
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // remove token
//   resetToken({ commit }) {
//     return new Promise(resolve => {
//       removeToken() // must remove  token  first
//       commit('RESET_STATE')
//       resolve()
//     })
//   }
// }
import { login } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
const state = {
  // 刷新页面时,先看看本地储存有没有备份
  // token: localStorage.getItem('token') || ''
  token: getToken() || ''
>>>>>>> 867ce04be0e223743ec7cefcbd98f9d0ab180ab4
}
const mutations = {
  setToken(state, data) {
    // 每当将token存入vuex的时候, 同时存一份到本地储存
    // localStorage.setItem('token', data)
    setToken(data)
    state.token = data
<<<<<<< HEAD
  },
  setProfile(state, data) {
    state.profile = data
  },
  removeToken(state) {
    removeToken()
    state.token = ''
  },
  removeProfile(state) {
    state.profile = {}
=======
>>>>>>> 867ce04be0e223743ec7cefcbd98f9d0ab180ab4
  }
}
const actions = {
  async login(store, data) {
    // 登录页应该将用户填写手机号和密码作为 data 传入
    const res = await login(data)
<<<<<<< HEAD
    console.log('调用接口之后')
    // console.log(res.data.data);
    store.commit('setToken', res)

    // 登录成功记录token的同时要记录时间戳, 用以主动介入token超时判断
    localStorage.setItem('loginTime', Date.now())
  },
  async getProfile(store) {
    const res = await getProfile()
    const detail = await getUserDetailById(res.userId)
    store.commit('setProfile', {
      ...res,
      ...detail
    })
  },
  logout(store) {
    store.commit('removeToken')
    store.commit('removeProfile')
    router.push('login')
=======
    // console.log(res.data.data);
    store.commit('setToken', res)
>>>>>>> 867ce04be0e223743ec7cefcbd98f9d0ab180ab4
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
